package com.syqc.monitor.ui.cost

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.luck.picture.lib.entity.LocalMedia
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.net.ServiceHelper
import com.syqc.utils.ToolsUtil
import okhttp3.MultipartBody
import kotlin.concurrent.thread

abstract class BaseFeeViewModel : AutoDisposeViewModel() {
    var carId = ""
    var dataType = 0
    var isSuccess = false

    var isUploading = false

    //备注
    var remark = ""

    //合计
    var feeTotal: Double = 0.0

    private var maxNum = 6
    var nowNum = maxNum
    fun setMaxPhotos() {
        maxNum = 1
    }

    //照片
    private val _photoLive = MutableLiveData<MutableList<String>>()
    var photoLive: LiveData<MutableList<String>> = _photoLive
    val photos = mutableListOf<String>("", "", "", "", "", "")
    private var emptySize = 0

    /**
     * 处理选中的照片
     *
     * @param medias
     */
    fun handlePhotos(medias: ArrayList<LocalMedia>?) {
        if (medias.isNullOrEmpty()) {
            _photoLive.postValue(photos)
            return
        }
        medias.forEach {
            photos[emptySize] = it.path
            emptySize++
        }
        nowNum = maxNum - emptySize
        _photoLive.postValue(photos)
    }

    /**
     * 处理单张照片
     *
     * @param medias
     */
    open fun handlePhoto(medias: ArrayList<LocalMedia>?) {

    }

    /**
     * 移除一个照片
     *
     * @param index
     */
    fun removePhoto(index: Int) {
        photos.removeAt(index)
        emptySize--
        photos.add("")
        nowNum = maxNum - emptySize
        _photoLive.postValue(photos)
    }

    //照片body
    protected fun getParts(): ArrayList<MultipartBody.Part?> {
        val parts = ArrayList<MultipartBody.Part?>()
        photos.forEachIndexed { index, path ->
            if (path.isNotEmpty()) {
                parts.add(ToolsUtil.getPartByPath("files", "fee_pic_$index", path))
            }
        }
        return parts
    }

    abstract fun isParamsOk(): Boolean


    val resultLive = MutableLiveData<Boolean>()

    /**
     * 提交数据
     *
     * @param feeType
     * @param entity
     */
    fun uploadFee(feeType: String, entity: String) {
        if (isUploading) return
        toastLoading(true)
        isUploading = true
        thread {
            ServiceHelper.getRepository().updateImgFee(feeType, entity, getParts())
                .autoDispose(this).subscribe({
                    isSuccess = true
                    isUploading = false
                    toastLoading(false)
                    resultLive.postValue(true)

                }, {
                    isUploading = false
                    toastLoading(false)
                    it.message?.run { toast(this) }
                    isSuccess = false
                    resultLive.postValue(false)
                })
        }
    }

    /**
     * 改变类型
     *
     * @param fee
     * @return
     */
    protected fun getDouble(fee: String): Double {
        return if (fee.isEmpty()) 0.0 else fee.toDouble()
    }

    /**
     * 清除数据
     *
     */
    open fun clearData() {
        nowNum = maxNum
        emptySize = 0
        photos.clear()
        for (i in 0 until maxNum) photos.add("")
        _photoLive.postValue(photos)
        remark = ""
    }
}